home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / text / font / ttfr.lha / ttfr / ttfr.c < prev    next >
C/C++ Source or Header  |  2000-11-12  |  4KB  |  177 lines

  1. /* ttfr.c */
  2. /* rename the given true type file to it fullname */
  3. /* removes spaces from name */
  4. /* by James S Perrin Time-stamp: <Sunday 12/11/00 13:38:09 nobody> */
  5.  
  6. /* v1.1 replaced dosman problem symbols <>":/ with . */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <strings.h>
  11.  
  12. /*#define DEBUG*/
  13.  
  14. typedef struct {
  15.   unsigned int scalarType;
  16.   unsigned short numTables;
  17.   unsigned short searchRange;
  18.   unsigned short entrySelector;
  19.   unsigned short rangeShift;
  20. } SubTable;
  21.  
  22. typedef struct {
  23.   unsigned int tag;
  24.   unsigned int checkSum;
  25.   unsigned int offset;
  26.   unsigned int length;  
  27. } TableDirectory;
  28.  
  29. typedef struct {
  30.   unsigned short platformID;
  31.   unsigned short platformSpecificID;
  32.   unsigned short languageID;
  33.   unsigned short nameID;
  34.   unsigned short length;
  35.   unsigned short offset;
  36. } NameRecord;
  37.  
  38. typedef struct {
  39.   unsigned short format;
  40.   unsigned short count;
  41.   unsigned short stringOffset;
  42.   char *names;
  43. } NameTable;
  44.  
  45. int main(int argc, char *argv[])
  46. {
  47.   int i, j, skip;
  48.   char *tagptr, *fullname;
  49.   FILE *fin;
  50.   
  51.   SubTable subTable;
  52.   TableDirectory tableDir;
  53.   NameTable nameTable;
  54.   NameRecord nameRecord;
  55.  
  56. #ifdef DEBUG
  57.   printf("sizeof(SubTable) = %d\n", sizeof(SubTable));
  58.   printf("sizeof(TableDirectory) = %d\n", sizeof(TableDirectory));
  59.   printf("sizeof(NameTable) = %d\n", sizeof(NameTable));
  60.   printf("sizeof(NameRecord) = %d\n", sizeof(NameRecord));
  61. #endif
  62.  
  63.   if(argc<2)
  64.   {
  65.     printf("%s <files...>\n", argv[0]);
  66.     exit(1);
  67.   }
  68.  
  69.   for(i=1; i<argc; i++)
  70.   {
  71.     if(fin=fopen(argv[i], "rb"))
  72.     {
  73.       /* read subTable */
  74.       fread(&subTable, sizeof(SubTable), 1, fin);
  75.  
  76. #ifdef DEBUG
  77.       printf("Num of Tables %d\n", subTable.numTables);
  78. #endif
  79.  
  80.       /* search for name table */
  81.       for(j=0; j<subTable.numTables; j++)
  82.       {
  83.     fread(&tableDir, sizeof(TableDirectory), 1, fin);
  84.     tagptr = (char *)&tableDir.tag;
  85.     
  86. #ifdef DEBUG
  87.       printf("TableDir %d tag %c%c%c%c\n", j, tagptr[0], tagptr[1],tagptr[2],tagptr[3]);
  88. #endif
  89.  
  90.     if(*(tagptr++)=='n' && *(tagptr++)=='a' &&
  91.        *(tagptr++)=='m' && *(tagptr++)=='e')
  92.       break;
  93.       }
  94.  
  95.       /* jump to name table in file */
  96.       fseek(fin, tableDir.offset, SEEK_SET);
  97.  
  98.       /* get name table */
  99.       fread(&nameTable.format, sizeof(unsigned short), 1, fin); 
  100.       fread(&nameTable.count, sizeof(unsigned short), 1, fin);
  101.       fread(&nameTable.stringOffset, sizeof(unsigned short), 1, fin);
  102.  
  103. #ifdef DEBUG
  104.       printf("Number of NameRecords %d\n", nameTable.count);
  105.       printf("Strings Offset %d\n", nameTable.stringOffset);
  106. #endif
  107.       
  108.       /* find full name record */
  109.       for(j=0; j<nameTable.count; j++)
  110.       {
  111.     fread(&nameRecord, sizeof(NameRecord), 1, fin);
  112.     
  113. #ifdef DEBUG
  114.       printf("NameRecord %d nameID %d\n", j, nameRecord.nameID);
  115. #endif
  116.  
  117.     if(nameRecord.nameID == 4)
  118.       break;
  119.       }
  120.  
  121.       /* skip remaining records */
  122.       fseek(fin, (nameTable.count-j-1) * sizeof(NameRecord), SEEK_CUR);
  123.  
  124.       /* go get fullname string */
  125.       fseek(fin, nameRecord.offset, SEEK_CUR);
  126.       
  127.       fullname = malloc(nameRecord.length+5); /* .ttf\0 */
  128.       
  129. #ifdef DEBUG
  130.       printf("NameRecord %d length %d\n", j, nameRecord.length);
  131.       printf("NameRecord %d offset %d\n", j, nameRecord.offset);
  132.       printf("NameRecord %d platfornID %d\n", j, nameRecord.platformID);
  133. #endif
  134.  
  135.       /* bodge 16bit unicode strings */
  136.       if(nameRecord.platformID == 0)
  137.     skip = 2;
  138.       else
  139.     skip = 1;
  140.       
  141.       for(j=0; j<nameRecord.length/skip; j++)
  142.       {
  143.     if(skip == 2) getc(fin);
  144.     fullname[j] = (char)getc(fin);
  145.       }
  146.       
  147.       fullname[j]='\0';
  148.  
  149.       /* Remove spaces */
  150.       for(j=0; j<nameRecord.length && fullname[j] != '\0'; j++)
  151.       {
  152.     if(fullname[j] == ' ')
  153.       strcpy(&fullname[j], &fullname[j+1]);
  154.     
  155.     if(fullname[j] == ':' || fullname[j] == '/' || fullname[j] == '<' || fullname[j] == '>' || fullname[j] == '"')
  156.       fullname[j] = '.'; 
  157.       }
  158.  
  159.       /* add extension */
  160.       strcat(fullname, ".ttf");
  161.  
  162.       /* rename original file */
  163.       printf("Renaming %s to %s\n", argv[i], fullname);
  164.       fclose(fin);
  165.       rename(argv[i], fullname);
  166.  
  167.       /* clean up */
  168.       free(fullname);
  169.     }
  170.     else
  171.      printf("Can't open file %s\n",argv[i]);
  172.  
  173.   }
  174.  
  175.   exit(0);
  176. }
  177.